<template>
  <BasicModal v-bind="$attrs"
    :width="800"
    :canFullscreen="false"
    :destroyOnClose="true"
    :maskClosable="false"
    @register="registerModal"
  >
<!--    <BasicForm @register="registerForm" >-->

<!--    </BasicForm>-->
    <Cascader :defaultValue="values" :options="cooperatorNameSelect" :load-data="coopSelectLoadData" stringToNumber/>

  </BasicModal>
</template>
<script lang="ts" name="AccountModal" setup>
import { computed, reactive, ref, toRefs, unref,nextTick } from "vue";
import { BasicModal, useModalInner } from "/@/components/Modal";
import { useMessage } from "/@/hooks/web/useMessage";
import { coopSelect } from "/@/views/full/business/business.api";
import { Cascader } from 'ant-design-vue';
const { createMessage: $message } = useMessage();
const isUpdate = ref(false);

// const accountSchema: FormSchema[] = [
//   {
//     label: '支付项目',
//     field: 'cId',
//     component: 'ApiCascader',
//     componentProps: {
//       api: coopSelect,
//       asyncFetchParamKey: 'pid',
//     },
//     defaultValue: [366,369,416],
//     colProps: {
//       span: 22,
//     },
//   },
// ]
//
// //表单配置
// const [registerForm, {resetFields, setFieldsValue}] = useForm({
//   labelWidth: 100,
//   schemas: accountSchema,
//   showActionButtonGroup: false
// });


let values = reactive<any>([366,369,416])
let cooperatorNameSelect = reactive<any>([])

//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {

  //重置表单
  //await resetFields();
  let record = {cId: [366,369,416] }

  nextTick(() => {
    coopSelectLoadData(false);
    // for(let item of values){
    //   console.log(item); //item指的的就是数组每一项的值。不是索引。
    //   coopSelectLoadData(item);
    // }
  })

  //表单赋值
  //await setFieldsValue({ ...record });

});

async function coopSelectLoadData(selectedOptions) {
  console.info("selectedOptions@@@@", selectedOptions)
  var params = {}
  var value = ''
  var targetOption:any = {}
  if (selectedOptions) {
    targetOption = selectedOptions[selectedOptions.length - 1]
    targetOption.loading = true
    value = targetOption.value
    if (value) {
      params = { pid: value }
    }
  } else {
    params = { values: '366,369,416,' }
  }

  console.info("cooperatorNameSelect@@", cooperatorNameSelect)

  await coopSelect(params).then((res) => {
    try {
      if (value) {
        targetOption.loading = false
        targetOption.children = res
        cooperatorNameSelect = [...cooperatorNameSelect]
      } else {
        Object.assign(cooperatorNameSelect, res)
      }
      console.info(value, res, cooperatorNameSelect)
    } catch (e) {
      alert('loadTree' + e)
      return false
    }
  }).finally()
}



</script>
